package com.example.springbootmqdemo.aajuc;

/**
 * JUC并发编程练习
 * @Author: Jam
 * @Create 2022/12/2 - 18:34
 * @Version 使用synchronized
 */
public class Thread1 {
    public static void main(String[] args) {
        TicketB ticket2  = new TicketB();

        //lambda表达式 (参数)->{代码}
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket2.sale();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket2.sale();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket2.sale();
            }
        }, "C").start();
    }
}
//使用synchronized
class TicketB{
    //属性 方法
    private  int number = 30;

    //卖票
    public  synchronized void sale(){
        if(number > 0){
            System.out.println(Thread.currentThread().getName()+"卖出了"+(number--)+"票，剩余："+number);
        }
    }
}